﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.UserCon.SysSetting
{
    public partial class uc_Main_Base_DocLevel_Mana : DevExpress.XtraEditors.XtraForm
    {
        public LeverString_SysTreeMainList _tl = new LeverString_SysTreeMainList();//Tree
        public LeverString_SysTreeMainList _ll = new LeverString_SysTreeMainList();//Level
        public string _LoadType = "";
        public uc_Main_Base_DocLevel_Mana()
        {
            InitializeComponent();
        }

        private void uc_Sys_Settings_Level_ManaForm_Load(object sender, EventArgs e)
        {
            RadioButton _rb=new RadioButton();
            flp_Func_0.Controls.Clear();
            flp_Func_1.Controls.Clear();
            flp_Func_2.Controls.Clear();
            gb_Str_0.Text = _tl.f_str_0;
            gb_Str_1.Text = _tl.f_str_1;
            gb_Str_2.Text = _tl.f_str_2;
            gb_Func_0.Text = _tl.f_func_0.Split(',')[0];
            gb_Func_1.Text = _tl.f_func_1.Split(',')[0];
            gb_Func_2.Text = _tl.f_func_2.Split(',')[0];
            te_Name.Text = _ll.f_name;
            te_Code.Text = _ll.f_code;
            te_Str_0.Text = _ll.f_str_0;
            te_Str_1.Text = _ll.f_str_1;
            te_Str_2.Text = _ll.f_str_2;
            if (_tl.f_func_0 != ",")
            {
                for (int i = 1; i < _tl.f_func_0.Split(',').Length; i++)
                {
                    _rb = new RadioButton();
                    _rb.Name = "rb_Func_0_" + (i - 1).ToString();
                    _rb.Tag = (i - 1).ToString();
                    _rb.Text = _tl.f_func_0.Split(',')[i];
                    _rb.Width = 200;
                    _rb.Height = 26;
                    if (_ll.f_func_0 == (i - 1).ToString())
                    {
                        _rb.Checked = true;
                    }
                    flp_Func_0.Controls.Add(_rb);
                }
            }
            if (_tl.f_func_1 != ",")
            {
                for (int i = 1; i < _tl.f_func_1.Split(',').Length; i++)
                {
                    _rb = new RadioButton();
                    _rb.Name = "rb_Func_1_" + (i - 1).ToString();
                    _rb.Tag = (i - 1).ToString();
                    _rb.Text = _tl.f_func_1.Split(',')[i];
                    _rb.Width = 200;
                    _rb.Height = 26;
                    if (_ll.f_func_1 == (i - 1).ToString())
                    {
                        _rb.Checked = true;
                    }
                    flp_Func_1.Controls.Add(_rb);
                }
            }
            if (_tl.f_func_2 != ",")
            {
                for (int i = 1; i < _tl.f_func_2.Split(',').Length; i++)
                {
                    _rb = new RadioButton();
                    _rb.Name = "rb_Func_2_" + (i - 1).ToString();
                    _rb.Tag = (i - 1).ToString();
                    _rb.Text = _tl.f_func_2.Split(',')[i];
                    _rb.Width = 200;
                    _rb.Height = 26;
                    if (_ll.f_func_2 == (i - 1).ToString())
                    {
                        _rb.Checked = true;
                    }
                    flp_Func_2.Controls.Add(_rb);
                }
            }
            if (_ll.f_type == "0")
            {
                rb_1.Checked = false;
                rb_0.Checked = true;
            }
            else
            {
                rb_0.Checked = false;
                rb_1.Checked = true;
            }
            if (_LoadType == "Add")
            {
                bt_Sure.Text = "新  增";
            }
            else if (_LoadType == "Sub_Add")
            {
                bt_Sure.Text = "新  增";
            }
            else if (_LoadType == "Edit")
            {
                bt_Sure.Text = "修  改";
            }
            else
            {
                bt_Sure.Text = "删  除";
            }
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string f_type = "0";
            string f_func_0 = "-1";
            string f_func_1 = "-1";
            string f_func_2 = "-1";
            if (gb_Func_0.Text != "-")
            {
                foreach (Control _c in flp_Func_0.Controls)
                {
                    if ((_c as RadioButton).Checked)
                    {
                        f_func_0 = (_c as RadioButton).Tag.ToString();
                    }
                }
            }
            if (gb_Func_1.Text != "-")
            {
                foreach (Control _c in flp_Func_1.Controls)
                {
                    if ((_c as RadioButton).Checked)
                    {
                        f_func_1 = (_c as RadioButton).Tag.ToString();
                    }
                }
            }
            if (gb_Func_2.Text != "-")
            {
                foreach (Control _c in flp_Func_2.Controls)
                {
                    if ((_c as RadioButton).Checked)
                    {
                        f_func_2 = (_c as RadioButton).Tag.ToString();
                    }
                }
            }
            if (rb_1.Checked)
            {
                f_type = "1";
            }
            if (_LoadType == "Add")
            {
                strSQL = "insert into TS_S_T_P_Tree (f_mainid,f_code,f_name,f_level,f_deep,f_type,f_str_0,f_str_1,f_str_2,f_func_0,f_func_1,f_func_2) values (" + _tl.f_id.Substring(1) + ",'" + te_Code.Text.Trim() + "','" + te_Name.Text.Trim() + "',',',0," + f_type + ",'" + te_Str_0.Text.Trim() + "','" + te_Str_1.Text.Trim() + "','" + te_Str_2.Text.Trim() + "','" + f_func_0 + "','" + f_func_1 + "','" + f_func_2 + "')";
            }
            else if (_LoadType == "Sub_Add")
            {
                strSQL = "insert into TS_S_T_P_Tree (f_mainid,f_code,f_name,f_level,f_deep,f_type,f_str_0,f_str_1,f_str_2,f_func_0,f_func_1,f_func_2) values (" + _tl.f_id.Substring(1) + ",'" + te_Code.Text.Trim() + "','" + te_Name.Text.Trim() + "','" + _ll.f_level + _ll.f_id.Substring(1) + ",'," + (Convert.ToInt32(_ll.f_deep) + 1).ToString() + "," + f_type + ",'" + te_Str_0.Text.Trim() + "','" + te_Str_1.Text.Trim() + "','" + te_Str_2.Text.Trim() + "','" + f_func_0 + "','" + f_func_1 + "','" + f_func_2 + "')";
            }
            else if (_LoadType == "Edit")
            {
                strSQL = "update TS_S_T_P_Tree set f_name='" + te_Name.Text.Trim() + "',f_code='" + te_Code.Text.Trim() + "',f_type=" + f_type + ",f_str_0='" + te_Str_0.Text.Trim() + "',f_str_1='" + te_Str_1.Text.Trim() + "',f_str_2='" + te_Str_2.Text.Trim() + "',f_func_0='" + f_func_0 + "',f_func_1='" + f_func_1 + "',f_func_2='" + f_func_2 + "',f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _ll.f_id.Substring(1);
            }
            else
            {
                //删除目录及子目录的目录数据表
                strSQL = "update TS_S_T_P_Tree set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + _ll.f_id.Substring(1) + " and f_del=0";
                strSQL += "update TS_S_T_P_Tree set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_level like '%," + _ll.f_id.Substring(1) + ",%' and f_del=0";
                //删除目录及子目录的目录资源表
                strSQL += ";update TS_S_T_P_Tree_Resource set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_mainid in (select f_id from TS_S_T_P_Tree where f_id=" + _ll.f_id.Substring(1) + " or f_level like '%," + _ll.f_id.Substring(1) + ",%') and f_del=0";
                //删除目录及子目录的目录关联表
                strSQL += ";update TS_S_T_P_Tree_Relation set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_tlid in (select f_id from TS_S_T_P_Tree where f_id=" + _ll.f_id.Substring(1) + " or f_level like '%," + _ll.f_id.Substring(1) + ",%') and f_del=0";
                //删除目录及子目录的目录授权表
                strSQL += ";update TS_S_T_S_Tree_Authority set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_menuid in (select f_id from TS_S_T_P_Tree where f_id=" + _ll.f_id.Substring(1) + " or f_level like '%," + _ll.f_id.Substring(1) + ",%') and f_del=0";
                //删除报表字段
                strSQL += ";update TS_S_T_P_Report_Field set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_rmid in (select f_id from TS_S_T_P_Tree where f_id=" + _ll.f_id.Substring(1) + " or f_level like '%," + _ll.f_id.Substring(1) + ",%') and f_del=0";
                //删除报表关联流程
                strSQL += ";update TS_S_T_P_Report_Tech set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_treeid in (select f_id from TS_S_T_P_Tree where f_id=" + _ll.f_id.Substring(1) + " or f_level like '%," + _ll.f_id.Substring(1) + ",%') and f_del=0";
                //删除报表关联统计字段
                strSQL += ";update TS_S_T_P_Report_Field_WP set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_rmid in (select f_id from TS_S_T_P_Tree where f_id=" + _ll.f_id.Substring(1) + " or f_level like '%," + _ll.f_id.Substring(1) + ",%') and f_del=0";
                //删除报表关联填报部门
                strSQL += ";update TS_S_T_P_Report_Dep set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_rmid in (select f_id from TS_S_T_P_Tree where f_id=" + _ll.f_id.Substring(1) + " or f_level like '%," + _ll.f_id.Substring(1) + ",%') and f_del=0";
                //删除报表字段填报授权
                strSQL += ";update TS_S_T_P_Report_Authority set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_filedid in(select f_id from TS_S_T_P_Report_Field where f_rmid in (select f_id from TS_S_T_P_Tree where f_id=" + _ll.f_id.Substring(1) + " or f_level like '%," + _ll.f_id.Substring(1) + ",%')) and f_del=0";
            }
            //MessageBox.Show(strSQL);
            TS_Win_Class.MySqlCom(strSQL);
            this.DialogResult = DialogResult.OK;
        }

        private void te_Name_EditValueChanged(object sender, EventArgs e)
        {
            if (te_Name.Text.Trim().Length > 0 && te_Str_0.Text.Trim().Length > 0 && te_Str_1.Text.Trim().Length > 0 && te_Str_2.Text.Trim().Length > 0 && te_Code.Text.Trim().Length > 0)
            {
                bt_Sure.Enabled = true;
            }
            else
            {
                bt_Sure.Enabled = false;
            }
        }
    }
}